package com.wn.cleandemo.utils;

import android.text.TextUtils;

import java.security.MessageDigest;

public class Md5Util {
    public static MessageDigest MD_MD5;
    private static String SPLI = ("hAAAa".replace("AAA", "")) + ("wBBBk".replace("BBB", ""));//hawk

    public static MessageDigest getMD5() {
        try {
            return MessageDigest.getInstance("MD5");
        } catch (Exception unused) {
            return null;
        }
    }

    public static long encrypt(String str) {
        long j2 = 0;
        if (str != null) {
            try {
                if (null == MD_MD5) {
                    MD_MD5 = getMD5();
                }
                MD_MD5.update(str.getBytes());
                byte[] digest = MD_MD5.digest();
                if (digest != null && digest.length == 16) {
                    long j3 = 0;
                    for (int i2 = 0; i2 < 8; i2++) {
                        j3 = (j3 << 8) | ((long) (digest[i2] & 255));
                    }
                    j2 = j3;
                }
            } catch (Exception unused) {
            }
        }
        MD_MD5.reset();
        return j2;
    }


    public static String encryptPath(String str) {
        if (str.startsWith("/")) {
            str = str.substring(1);
        }
        if (TextUtils.isEmpty(str)) {
            return "";
        }
        String[] split = str.split("/");
        StringBuilder sb = new StringBuilder();
        for (int i2 = 0; i2 < split.length; i2++) {
            if (!TextUtils.isEmpty(split[i2])) {
                sb.append(MD5toHex(addddd(split[i2]) + SPLI));
            }
            if (i2 != split.length - 1) {
                sb.append('+');
            }
        }
        return sb.toString();
    }

    private static String MD5toHex(String str) {
        try {
            MessageDigest instance = MessageDigest.getInstance("MD5");
            instance.update(str.getBytes());
            byte[] digest = instance.digest();
            if (digest == null) {
                return null;
            }
            int length = digest.length;
            StringBuilder sb = new StringBuilder(length * 2);
            for (int i2 = 0; i2 < length; i2++) {
                sb.append("0123456789abcdef".charAt((digest[i2] & 240) >>> 4));
                sb.append("0123456789abcdef".charAt(digest[i2] & 15));
            }
            return sb.toString();
        } catch (Exception e2) {
            e2.printStackTrace();
            return null;
        }
    }

    private static String addddd(String str) {
        char[] charArray;
        if (TextUtils.isEmpty(str) || (charArray = str.toCharArray()) == null) {
            return str;
        }
        for (int i2 = 0; i2 < charArray.length; i2++) {
            char c2 = charArray[i2];
            if ('A' <= c2 && c2 <= 'Z') {
                charArray[i2] = (char) ((c2 + 'a') - 65);
            }
        }
        return String.valueOf(charArray);
    }

}
